﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace nGSO.KnapsackProblem
{
    public class KPFitnessCalulator : IMultimodalFun
    {
        private KpConfigElement kpConfig;

        public KPFitnessCalulator(KpConfigElement kpConfig)
        {
            this.kpConfig = kpConfig;            
        }

        public double EvaluateMultimodalFunction(Agent agent, double[] loc)
        {
            double fitness = 0;
            int totalWeight = 0;

            for (int i = 0; i < kpConfig.Dims; i++)
            {
                int cost = kpConfig.Costs[i];
                fitness += cost * agent.Loc[i];

                totalWeight += (int)agent.Loc[i] * kpConfig.Weights[i];
            }

            if (totalWeight > kpConfig.MaxCapacity) // peantly for overweight
            {
                fitness = (fitness * kpConfig.MaxCapacity) / totalWeight;
            }

            return fitness;
        }
    }
}
